package com.sx.basic.serializer;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.math.BigDecimal;

/**
 * @author AdinZ
 * @date 2020/7/2 10:04
 */
public class BigDecimalToStringSerializer  implements ObjectSerializer {

    public static final BigDecimalToStringSerializer instance = new BigDecimalToStringSerializer();
    public BigDecimalToStringSerializer() {
    }

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        SerializeWriter out = serializer.out;
        if (object == null) {
            out.writeNull();
            return;
        }
        if(object instanceof BigDecimal){
            out.writeString(((BigDecimal) object).toPlainString());
        }else out.writeString(object.toString());
    }
}
